using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Web.UI;
using System.Web.UI.WebControls;

using DotNetNuke.Common.Utilities;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Entities.Modules.Actions;
using DotNetNuke.Services.Exceptions;
using DotNetNuke.Services.Localization;

namespace PT.AddressInfo
{
    /// -----------------------------------------------------------------------------
    /// <summary>
    /// The ViewAddressInfo class displays the content
    /// </summary>
    /// <remarks>
    /// </remarks>
    /// <history>
    /// </history>
    /// -----------------------------------------------------------------------------
    partial class ViewAddressInfo : PortalModuleBase, IActionable
    {

        #region Private Members

        private string strTemplate;

        #endregion

        #region Public Methods

        public bool DisplayAudit()
        {
            bool retValue = false;

            if ((string)Settings["auditinfo"] == "Y")
            {
                retValue = true;
            }

            return retValue;
        }

        #endregion

        #region Event Handlers

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Page_Load runs when the control is loaded
        /// </summary>
        /// <remarks>
        /// </remarks>
        /// <history>
        /// </history>
        /// -----------------------------------------------------------------------------
        protected void Page_Load(System.Object sender, System.EventArgs e)
        {
            try
            {
                if (!Page.IsPostBack)
                {
                    if (((string)Settings["template"] != null) && ((string)Settings["template"] != ""))
                        strTemplate = (string)Settings["template"];
                    else
                        strTemplate = Localization.GetString("Template.Text", LocalResourceFile);

                    AddressInfoController objAddressInfos = new AddressInfoController();
                    List<AddressInfoInfo> colAddressInfos;

                    //get the content from the AddressInfo table
                    colAddressInfos = objAddressInfos.GetAddressInfos(ModuleId);

                    if (colAddressInfos.Count == 0)
                    {
                        //add the content to the AddressInfo table
                        AddressInfoInfo objAddressInfo = new AddressInfoInfo();
                        objAddressInfo.ModuleId = ModuleId;
                        objAddressInfo.Content = Localization.GetString("DefaultContent", LocalResourceFile);
                        objAddressInfo.CreatedByUser = this.UserId;
                        objAddressInfos.AddAddressInfo(objAddressInfo);

                        //get the content from the AddressInfo table
                        colAddressInfos = objAddressInfos.GetAddressInfos(ModuleId);
                    }

                    //bind the content to the repeater
                    lstContent.DataSource = colAddressInfos;
                    lstContent.DataBind();
                }
            }
            catch (Exception exc) //Module failed to load
            {
                Exceptions.ProcessModuleLoadException(this, exc);
            }

        }

        protected void lstContent_ItemDataBound(System.Object sender, System.Web.UI.WebControls.DataListItemEventArgs e)
        {
            string strContent = strTemplate;
            string strValue = Null.NullString;

            //add content to template
            ArrayList objProperties = CBO.GetPropertyInfo(typeof(AddressInfoInfo));
            int intProperty;
            foreach (PropertyInfo objPropertyInfo in objProperties)
            {
                if (strContent.IndexOf("[" + objPropertyInfo.Name.ToUpper() + "]") != -1)
                {
                    strValue = Server.HtmlDecode(DataBinder.Eval(e.Item.DataItem, objPropertyInfo.Name).ToString());
                    strContent = strContent.Replace("[" + objPropertyInfo.Name.ToUpper() + "]", strValue);
                }
            }

            //assign the content
            Label lblContent = (Label)e.Item.FindControl("lblContent");
            lblContent.Text = strContent;
        }

        #endregion

        #region Optional Interfaces

        public ModuleActionCollection ModuleActions
        {
            get
            {
                ModuleActionCollection Actions = new ModuleActionCollection();
                return Actions;
            }
        }

        #endregion

    }
}

